<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: forum_index.php 29580 2012-04-20 02:53:59Z svn_project_zhangjie $
 */
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(!$_G['group']['allowvote'] ) {
	showmessage('group_nopermission', NULL, array('grouptitle' => $_G['group']['grouptitle']), array('login' => 1));
} elseif(!empty($thread['closed'])) {
	showmessage('thread_poll_closed', NULL, array(), array('login' => 1));
} elseif(empty($_GET['pollanswers'])) {
	showmessage('thread_poll_invalid', NULL, array(), array('login' => 1));
}

foreach( $_POST['pollanswers'] as $tid => $pollid ){
	doPoll( $tid, $pollid );
}
showmessage( '感谢您的参与', $_SERVER['HTTP_REFERER'] );

function doPoll( $tid, $pollid ){
	global $_G;
	$pollarray		= C::t('forum_poll')->fetch( $tid );
	$overt			= $pollarray['overt'];
	$voterids		= $_G['uid'] ? $_G['uid'] : $_G['clientip'];
	$polloptionid	= array();

	if( !$pollarray ){
		return false;
	}elseif( $pollarray['expiration'] && $pollarray['expiration'] < TIMESTAMP ){
		return false;
	}


	$query			= C::t('forum_polloption')->fetch_all_by_tid( $tid );

	foreach( $query as $pollarray ){
		if( strexists( "\t".$pollarray['voterids']."\t", "\t".$voterids."\t" ) ){
			return false;
		}
		$polloptionid[] = $pollarray['polloptionid'];
	}

	$polloptionids	= '';
	if( !in_array( $pollid, $polloptionid ) )
		return false;
	$polloptionids[] = $pollid;

	C::t('forum_polloption')->update_vote( $polloptionids, $voterids."\t", 1 );
	C::t('forum_thread')->update( $tid, array( 'lastpost' => $_G['timestamp'] ), true );
	C::t('forum_poll')->update_vote( $tid );
	C::t('forum_pollvoter')->insert( array(
		'tid'		=> $tid,
		'uid'		=> $_G['uid'],
		'username'	=> $_G['username'],
		'options'	=> implode("\t", $_GET['pollanswers']),
		'dateline'	=> $_G['timestamp'],
		) );
	updatecreditbyaction( 'joinpoll' );

	$space = array();
	space_merge( $space, 'field_home' );

	if( $overt && !empty( $space['privacy']['feed']['newreply'] ) ){
		$feed['icon']			= 'poll';
		$feed['title_template']	= 'feed_thread_votepoll_title';
		$feed['title_data']		= array(
			'subject'	=> "<a href=\"forum.php?mod=viewthread&tid=$_G[tid]\">$thread[subject]</a>",
			'author'	=> "<a href=\"home.php?mod=space&uid=$thread[authorid]\">$thread[author]</a>",
			'hash_data'	=> "tid{$_G[tid]}"
		);
		$feed['id']		= $tid;
		$feed['idtype']	= 'tid';
		postfeed( $feed );
	}
	return true;
}

?>